//
//  AppDelegate.swift
//  微博
//
//  Created by 余仿 on 16/4/6.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        window = UIWindow(frame: UIScreen.mainScreen().bounds)

        //添加观察者
        registerObserver()
        //根控制器是什么需要根据一个条件判断
        window?.rootViewController = defaultRootController()

        window?.makeKeyAndVisible()

        return true
    }

    /**
     注册通知的观察者
     */
    func registerObserver(){

         NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeRootController:", name: YFSwitchRootController, object: nil)
    }

    /**
     AppDelegate收到切换根视图控制器的消息,切换控制器的方法
     */
    func changeRootController(notification:NSNotification){

        window?.rootViewController = notification.object != nil ? YFMainViewController() : YFWelComeViewController()
    }

    /**
     程序一启动就应该判断是否是登录状态

     - returns: 根控制器
     */
    func defaultRootController() -> UIViewController{

        return YFViewModel.sharedViewModels.userLogIn ? YFMainViewController() : YFWelComeViewController()
    }

}

